<template>
    <div class="his-more-pop">
        <div class="pop-bg"></div>
        <div class="pop-wrap">
            <div class="pop-title"><span class="close" @click="closeSelect">&times;</span>今日单多选发布</div>
            <div class="pop-con no-txt" v-if="newLis.length == 0">今日暂无未发布的HIS单</div>
            <div class="pop-con" v-if="newLis.length > 0">
                <div class="pop-con-lis">
                    <table>
                        <thead>
                        <tr>
                            <td>姓名</td>
                            <td>床号</td>
                            <td>预约时间</td>
                            <td>扫码编号</td>
                            <td>输送类型</td>
                            <td>输送起点</td>
                            <td>输送终点</td>
                        </tr>
                        </thead>
                        <tbody>
                        <template v-for="mitem in newLis">
                            <tr v-for="(item,index) in mitem.lis" :class="{'curr':item.select}" @click="selectHis(item)">
                                <td v-if="index == 0" :rowspan="mitem.lis.length" class="name">{{item.name}}</td>
                                <td v-if="index == 0" :rowspan="mitem.lis.length" class="bed">{{item.bedno}}</td>
                                <td>{{item.fixedtime}}</td>
                                <td>{{item.guid}}</td>
                                <td>{{item.dept}}</td>
                                <td>{{item.askdept}}</td>
                                <td>{{item.address}}</td>
                            </tr>
                        </template>

                        </tbody>
                    </table>
                </div>
                <div class="handle-btn">
                    <button class="btn ok-btn" @click="sureSelect">确定多单发布</button>
                    <button class="btn close-btn" @click="closeSelect">关闭</button>
                    <div>
                        <span>已选择：{{idLis.length}}</span>
                        <button class="btn select-btn" @click="allSelect"><span class="selectSpan"></span>全选</button>
                        <button class="btn select-btn" v-if="idLis.length > 0 && idLis.length < newLis.length" @click="invertSelect"><span class="selectSpan"></span>反选</button>
                        <button class="btn select-btn" v-if="idLis.length > 0" @click="cancelSelect">取消已选</button>
                        <button class="btn select-btn" v-if="idLis.length > 0" @click="handlePrintHis">打印已选</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    export default{
        props:{
            list:{
                type:Array,
                default:function(){
                    return []
                }
            },
            offDutys:''
        },
        data(){
            return{
                newLis:[],
                idLis:[],
                isAll:false
            }
        },
        methods:{
            selectHis(val){
                val.select = !val.select;
                let ind = this.idLis.indexOf(Number(val.id));
                if(ind == -1){
                    this.idLis.push(Number(val.id));
                }else {
                    this.idLis.splice(ind,1);
                }
                this.idLis = Array.from(new Set(this.idLis))
            },
            allSelect(){
                this.setSelect(true);
            },
            setSelect(str){
                this.idLis = []
                for(var val of this.newLis){
                    val.lis.forEach(v=>{
                        v.select = str;
                        if(v.select){
                            this.idLis.push(Number(val.id));
                        }
                    })
                }
            },
            invertSelect(){
                this.idLis = []
                for(var val of this.newLis){
                    val.lis.forEach(v=>{
                        v.select = !v.select;
                        if(v.select){
                            this.idLis.push(Number(val.id));
                        }
                    })
                }
            },
            cancelSelect(){
                this.setSelect(false);
            },
            //
            closeSelect(){
                this.idLis = [];
                this.$emit('close');
            },
            sureSelect(){
                if(this.offDutys){
                    this.$Message({type:'error',message:'调度员暂不接单，请改用电话方式联系'});
                    return
                }else {
                    let l = this.idLis.length;
                    if(l == 0){
                        this.$Message({type:'error',message:'请选择HIS单'});
                        return
                    }
                    this.$emit('sure',this.idLis);
                    this.closeSelect();
                }
            },
            //打印
            handlePrintHis(){
                let plis = []
                this.newLis.forEach(v=>{
                    v.lis.forEach(val=>{
                        if(val.select){
                            plis.push(val)
                        }
                    })
                })
                this.$emit('printfn',plis)
            }
        },
        mounted() {
            setTimeout(function(){
                this.newLis = this.list
            }.bind(this),50);

        }
    }
</script>
<style scoped lang="less">
    @import "../assets/less/his-more.less";
</style>
